from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QPushButton, QSlider, QLabel, QWidget


class Sound(QWidget):
    """管理声音的类"""
    def __init__(self,MP3Player):
        super().__init__()
        #定义属性
        self.player = MP3Player.player
        self.soundBtn = QPushButton(self)
        self.soundslider = QSlider(Qt.Horizontal, self)
        self.soundvalueLabel = QLabel('80')
        self.isMuted = False
        #初始化
        self._initialization()
        #SingalSlot
        self.soundslider.valueChanged.connect(self.MoveSoundSlider)
        self.soundBtn.clicked.connect(self.Setsound)

    def _initialization(self):
        """初始化"""
        #音量初始化
        self.soundslider.setMinimum(0)
        self.soundslider.setMaximum(100)
        self.soundslider.setValue(80)
        #图案
        self.soundBtn.setStyleSheet("QPushButton{border-image: url(:/resource/images/sound.png)}")
        self.soundBtn.setFixedSize(32, 32)
        self.soundvalueLabel.setFixedSize(32, 32)
        self.soundvalueLabel.setStyleSheet("QLabel{font:20px;}")

    def Setsound(self):
        """开启或关闭声音"""
        if self.isMuted:
            self.isMuted = False
            self.player.setMuted(False)
            self.soundBtn.setStyleSheet("QPushButton{border-image: url(:/resource/images/sound.png)}")
        elif not self.isMuted:
            self.isMuted = True
            self.player.setMuted(True)
            self.soundBtn.setStyleSheet("QPushButton{border-image: url(:/resource/images/nosound.png)}")

    def MoveSoundSlider(self):
        """移动音量滑条"""
        soundvalue = self.soundslider.value()
        self.player.setVolume(soundvalue)
        self.soundvalueLabel.setText(str(soundvalue))
